﻿using EYangFrame.DataAccess.Global;
using EYangFramework.Business.SystemManager.Interfaces;
using EYangFramework.Common.Ninject;
using SMES.Common;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace EYangFramework.CommonForm
{
    public partial class frm_ModifyPassword : Form
    {
        public frm_ModifyPassword()
        {
            InitializeComponent();
        }

        private void btnModifyPasswrod_Click(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtOldPassword.Text) || string.IsNullOrEmpty(txtNewPassword.Text))
            {
                MessageBox.Show("请输入原密码及新密码!");
                ClearPassword();
                return;
            }
            if (txtOldPassword.Text.Trim() == txtNewPassword.Text.Trim())
            {
                MessageBox.Show("新密码不能与旧密码相同!");
                ClearPassword();
                return;
            }
            if (txtNewPassword.Text.Trim() != txtConfirmPassword.Text.Trim())
            {
                MessageBox.Show("两次输入密码不同,请重新输入!");
                ClearPassword();
                return;
            }
            string encryNewPassword = CEncoder.encryptMD5(txtNewPassword.Text.Trim());
            string encryOldPassword = CEncoder.encryptMD5(txtOldPassword.Text.Trim());
            if (encryOldPassword != GlobalInfo.CurrentUser.PASSWORD)
            {
                MessageBox.Show("原密码输入错误,请重新输入!");
                ClearPassword();
                return;
            }
            using (IBLLUser bLLUser = NinjectProvider.NinjectCore.GetService<IBLLUser>())
            {
                var currentUser = bLLUser.GetBusinessByID(GlobalInfo.CurrentUser.ID);
                currentUser.PASSWORD = encryNewPassword;
                GlobalInfo.CurrentUser.PASSWORD = encryNewPassword;
                currentUser.CHANGEPASSWORD = true;
                bool success = bLLUser.UpdateUser(currentUser);
                if (success)
                {
                    MessageBox.Show("密码修改成功!");
                    this.Close();
                }
            }
        }
        private void ClearPassword()
        {
            txtOldPassword.Text = "";
            txtNewPassword.Text = "";
            txtConfirmPassword.Text = "";
        }

        private void frmModifyPassword_Load(object sender, EventArgs e)
        {
            groupMain.Text = $"您正在修改{GlobalInfo.CurrentUser.USERNAME}的密码!";
        }
    }
}
